package com.dwxt.mall.admin.controller;

import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.dwxt.mall.admin.service.OrderServiceAdmin;
import com.dwxt.mall.entity.PayOrder;
import com.dwxt.mall.entity.vo.OrderVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RequestMapping("/mall/order")
@RestController
public class OrderControllerAdmin {
    @Autowired
    private OrderServiceAdmin orderServiceAdmin;

    @RequestMapping("/selectOrderPage")
    public BaseResult selectOrderPage(Integer pageNum, Integer pageSize, PayOrder order, String start_time,
                                      String end_time, Integer isSuccess) {
        PageHelper.startPage(pageNum, pageSize);
        List<PayOrder> orderList = orderServiceAdmin.selectOrderList(order, start_time, end_time, isSuccess);
        PageInfo<PayOrder> pageInfo = new PageInfo<>(orderList);
        return BaseResult.ok(pageInfo);
    }

    @RequestMapping("/exportOrder")
    public void exportOrder(HttpServletResponse response, PayOrder order, String start_time,
                            String end_time, Integer isSuccess) {
        orderServiceAdmin.exportOrder(response, order, start_time, end_time, isSuccess);
    }

    //发货
    @RequestMapping("/ship")
    public BaseResult ship(String id) {
        int i = orderServiceAdmin.ship(id);
        return i > 0 ? BaseResult.ok() : BaseResult.build(StatusCode.SHIP_FAIL);
    }

    /**
     * 后台查看订单详情
     *
     * @param id
     * @return
     */
    @RequestMapping("/selectOrder")
    public BaseResult selectOrder(String id) {
        PayOrder orderVO = orderServiceAdmin.selectOrder(id);
        return BaseResult.ok(orderVO);
    }

    //审核是否退款
    @RequestMapping("/refund")
    public BaseResult refund(String id, String reason, String status) throws Exception {
        return orderServiceAdmin.refund(id, reason, status);
    }

    @RequestMapping("/selectOrderStatistics")
    public BaseResult selectOrderStatistics(Integer year, Integer month) {
        return orderServiceAdmin.selectOrderStatistics(year, month);
    }


    @RequestMapping("/orderStatistics")
    public BaseResult orderStatistics(String startTime, String endTime) {
        return orderServiceAdmin.orderStatistics(startTime, endTime);
    }


    @RequestMapping("/exportOrderCategory")
    public void exportOrderCategory(HttpServletResponse response, String startTime, String endTime) {
        orderServiceAdmin.exportOrderCategory(response, startTime, endTime);
    }
}
